#### substitution 
css2<- read.csv("css05_cv.csv")
css2$part<- css2$cash + css2$vouchers
css2$part[css2$part==2]<-1
css2$part<- css2$part*100
css2$voucher_yr<-0
css2$voucher_yr[css2$elec_year>2016]<-1
css2$iswhiteperson<- 0
css2$iswhiteperson[css2$race=="European"]<- 1
css2$iswhiteperson[is.na(css2$race)]<- NA
css2$iswhiteperson[css2$race==""]<- NA

css2$income<- as.numeric(substr(css2$income,2,length(css2$income)))
css2$income_perc<- ceiling(ecdf(css2$income)(css2$income)*10)
css2$highinc<-0
css2$highinc[css2$income>100000]<-1
css2$highinc[is.na(css2$income)]<-NA
css2$highvote<-0
css2$highvote[css2$sums>13]<-1
css2$isdem<- 0
css2$isdem[css2$party=="Democratic"]<- 1
css2$isdem[is.na(css2$party)]<- NA


#pre-post numbers
dons15<- subset(css2$id, css2$part==100 & css2$elec_year<=2015)
dons15<- subset(css2, css2$id %in% dons15)
dons15<- subset(dons15, dons15$elec_year>=2017)

pp<- aggregate(part~income_perc, data=dons15, mean)
cashpp<- aggregate(100*cash~income_perc, data=dons15, mean)
voucherpp<- aggregate(100*vouchers~income_perc, data=dons15, mean)
df<- left_join(pp, cashpp)
df<- left_join(df, voucherpp)

names(df)<- c("income_perc","total_part","cash_part","voucher_part")
df$cash_and_voucher<- -1 * (df$total_part - df$cash_part - df$voucher_part)
df$cash_only<- df$cash_part - df$cash_and_voucher
df$voucher_only<- df$voucher_part - df$cash_and_voucher
df<- pivot_longer(df, -c(income_perc, cash_part, voucher_part, cash_and_voucher), values_to = "Value", names_to = "Type")

gg_subs<- ggplot(df, aes(x=income_perc, y=Value, fill=Type, color=as.factor(Type), linetype=as.factor(Type))) + geom_line()+ geom_point() + 
  geom_label(  label="Voucher only", x=3,  y=15.5,  label.size = .2,  color = "black",  fill="light blue")+
  geom_label(  label="Cash only", x=3,  y=7,  label.size = .2,  color = "black",  fill="orange")+
  geom_label(  label="Any contributions", x=3,  y=29,  label.size = .2,  color = "black",  fill="light green") +theme(legend.position = "none")+
  ggtitle("Voucher-era contributions among pre-voucher donors")+ ylab("Percent  re-donating")+xlab("Income decile")+ ylim(0,36)+ 
  scale_x_continuous(breaks = c(1:10))
gg_subs #fig a5


#2013-2015 numbers
dons15<- subset(css2$id, css2$part==100 & css2$elec_year<=2013)
dons15<- subset(css2, css2$id %in% dons15)
dons15<- subset(dons15, dons15$elec_year==2015)
pp<- aggregate(part~income_perc, data=dons15, mean)
pp$years<- "13_15"

gg_subs1<- ggplot(pp, aes(x=income_perc, y=part)) + geom_line()+ geom_point() +ylim(15,30)+scale_x_continuous(breaks = c(1:10))+
  ggtitle("2015 contributions among 2005-2013 donors")+ ylab("Percent  re-donating")+xlab("Income decile")
gg_subs1 #fig a6
